cmake_minimum_required(VERSION 3.12.4)

project(vulnerability_scanner)

enable_testing()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_definitions(-DPROMISE_TYPE=PromiseType::NORMAL)

if (NOT SRC_FOLDER)
    get_filename_component(SRC_FOLDER     ${CMAKE_SOURCE_DIR}/../../ ABSOLUTE)
endif (NOT SRC_FOLDER)

if (NOT SHARED_MODULES)
    get_filename_component(SHARED_MODULES ${SRC_FOLDER}/shared_modules/ ABSOLUTE)
endif (NOT SHARED_MODULES)

# If VERSION and REVISION are manually set, use them.

if (VERSION AND REVISION)
    add_definitions(-DVERSION="${VERSION}")
    add_definitions(-DREVISION="${REVISION}")
endif ()

# Include directories
include_directories(include)
include_directories(src/databaseFeedManager)
include_directories(src/policyManager)

include_directories(${SRC_FOLDER})
include_directories(${SRC_FOLDER}/headers)
include_directories(${SRC_FOLDER}/external/cJSON)
include_directories(${SRC_FOLDER}/external/nlohmann)
include_directories(${SRC_FOLDER}/external/rocksdb/include)
include_directories(${SRC_FOLDER}/external/flatbuffers/include)
include_directories(${SRC_FOLDER}/external/libarchive/libarchive/)
include_directories(${SRC_FOLDER}/external/lzma/src/liblzma/api)
include_directories(${SRC_FOLDER}/wazuh_modules/)
include_directories(${SRC_FOLDER}/wazuh_modules/vulnerability_scanner/src/scanOrchestrator/)

include_directories(${SHARED_MODULES}/common)
include_directories(${SHARED_MODULES}/utils)
include_directories(${SHARED_MODULES}/router/include)
include_directories(${SHARED_MODULES}/content_manager/include)
include_directories(${SHARED_MODULES}/indexer_connector/include)
include_directories(${SHARED_MODULES}/http-request/include)

# Link directories
link_directories(${SRC_FOLDER}/external/rocksdb/build)
link_directories(${SHARED_MODULES}/router/build)
link_directories(${SHARED_MODULES}/content_manager/build)
link_directories(${SHARED_MODULES}/indexer_connector/build)
link_directories(${SHARED_MODULES}/http-request/build)
link_directories(${SRC_FOLDER}/external/flatbuffers/build/)

list(APPEND Schemas
            cve5
            vulnerabilityCandidate
            vulnerabilityDescription
            vulnerabilityRemediations
            packageTranslation
            messageBuffer
)

message("Compiling schemas")
add_custom_target(compile_schemas)
foreach(schema IN LISTS Schemas)
  set(FILE "./schemas/${schema}.fbs")
  set(VULNERABILITY_OUTPUT_HEADER_GENERATED "${CMAKE_CURRENT_SOURCE_DIR}/include/${schema}_generated.h")
  set(VULNERABILITY_OUTPUT_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/${schema}_schema.h")

  add_custom_command(
      OUTPUT "${VULNERABILITY_OUTPUT_HEADER_GENERATED}"
      COMMAND ${SRC_FOLDER}/external/flatbuffers/build/flatc
      ARGS --cpp
      ARGS -o "${CMAKE_CURRENT_SOURCE_DIR}/include" "${FILE}"
      ARGS --no-warnings
      COMMENT "Building C++ header for ${schema} schema."
      DEPENDS ${FILE}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

  add_custom_command(
      OUTPUT "${VULNERABILITY_OUTPUT_HEADER}"
      COMMAND bash -c "echo -e '// This file was generated from ${FILE} , do not modify \\n#ifndef ${schema}_HEADER\\n#define ${schema}_HEADER\\nauto constexpr ${schema}_SCHEMA = R\"('`cat ${FILE}`')\" ;\\n#endif // ${schema}_HEADER\\n ' > ./include/${schema}_schema.h"
      COMMENT "Creating header from schema file: '${schema}'"
      DEPENDS ${VULNERABILITY_OUTPUT_HEADER_GENERATED}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      VERBATIM)

  add_custom_target(${schema}_schema_target DEPENDS ${VULNERABILITY_OUTPUT_HEADER})
  add_dependencies(compile_schemas ${schema}_schema_target)
endforeach()

add_subdirectory("src/scanOrchestrator")
add_subdirectory("src/databaseFeedManager")

file(GLOB VULNERABILITY_SCANNER_SRC
    "src/*.cpp"
    )

add_library(vulnerability_scanner SHARED
    ${VULNERABILITY_SCANNER_SRC}
    )

add_dependencies(database_feed compile_schemas) #Add a dependency between top-level targets.

target_link_libraries(vulnerability_scanner scan_orchestrator database_feed router content_manager indexer_connector router gcc_s flatbuffers)

set_target_properties(vulnerability_scanner PROPERTIES
        BUILD_RPATH_USE_ORIGIN TRUE
        BUILD_RPATH "$ORIGIN/../lib"
)

add_subdirectory(testtool)
if(UNIT_TEST)
  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    target_link_libraries(vulnerability_scanner -fprofile-arcs)
  else()
    target_link_libraries(vulnerability_scanner gcov)
  endif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")

  add_subdirectory(tests)
else()
  if(FSANITIZE)
    set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,leak,undefined")
  endif(FSANITIZE)
endif(UNIT_TEST)
